package com.minglead.springboot.platform.sys.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import com.minglead.springboot.common.utils.CacheUtils;
import com.minglead.springboot.common.utils.SpringContextHolder;
import com.minglead.springboot.platform.sys.dao.OfficeDao;
import com.minglead.springboot.platform.sys.entity.Dict;
import com.minglead.springboot.platform.sys.entity.Office;
import com.minglead.springboot.platform.sys.entity.Role;
import com.minglead.springboot.platform.sys.entity.User;


/**
 * 机构管理工具类
 **/
public class OfficeUtils {
	private static OfficeDao officeDao=SpringContextHolder.getBean(OfficeDao.class);
/*	private static OfficeService officeService=SpringContextHolder.getBean(OfficeService.class);*/
	private static final String CACHE_OFFICE_MAP="officeMap";
	public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";
	public static String getOfficeName(String value, String type, String defaultValue){
		if (type!=null && StringUtils.isNotBlank(value)){
			for (Office office : getOffice(type)){
				if (type.equals(office.getType()) && value.equals(office.getId())){
					return office.getName();
				}
			}
		}
		return defaultValue;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Office> getOffice(String type){
		User user=UserUtils.getUser();
		if(!user.isAdmin()&&user.getRoleList()!=null){
			for(Role r:user.getRoleList()){
				if(Role.DataScopeEnum.data_scope_self.name().toString().equals(r.getDataScope())){
					return  officeDao.findAllList(user.getOffice());
				}
			}
		}
		
		Map<String, List<Office>> officeMap = (Map<String, List<Office>>)CacheUtils.get(CACHE_OFFICE_MAP);
		if (officeMap==null){
			officeMap = new HashMap<String, List<Office>>();
			for (Office office : officeDao.findAllList(new Office())){
				List<Office> officeList = officeMap.get(office.getType());
				if (officeList != null){
					officeList.add(office);
				}else{
					List<Office> list0 = new ArrayList<Office>();
					list0.add(office);
					
					officeMap.put(office.getType(), list0);
				}
			}
			CacheUtils.put(CACHE_OFFICE_MAP, officeMap);
		}
		List<Office> officeList = officeMap.get(type);
		if (officeList == null){
			officeList = new ArrayList<Office>();
		}
		return officeList;
	}
	
	
	/**
	 * 带缓存，根据code 查询机构，速度更快
	 * @param code 不能为null
	 * @return
	 */
	public static Office getOfficeByCode(String code){
		List<Office> allOfficeList = getOfficeAllList(true);
		for(Office o:allOfficeList){
			if(code.equals(o.getCode()))
				return o;
		}
		return null;
	}
	/**
	 * 带缓存，根据id获取机构，速度更快
	 * @param id
	 * @return
	 */
	public static Office getOfficeById(String id){
		List<Office> allOfficeList = getOfficeAllList(false);
		for(Office o:allOfficeList){
			if(id.equals(o.getId()))
				return o;
		}
		return null;
	}
	
	/**
	 * 获取所有机构；机构更新的时候、第一个用户登录，刷新此方法
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Office> getOfficeAllList(Boolean refresh){
		List<Office> officeList = new ArrayList<Office>();
		if(refresh){
			officeList = officeDao.findAllList(new Office());
			CacheUtils.put(CACHE_OFFICE_ALL_LIST, officeList);
		}else{
			officeList = (List<Office>)CacheUtils.get(CACHE_OFFICE_ALL_LIST);
			if(officeList==null){
				officeList = officeDao.findAllList(new Office());
				CacheUtils.put(CACHE_OFFICE_ALL_LIST, officeList);	
			}
				
		}

		return officeList;
	}
	
}